﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ClassLibrary.Data;
using ClassLibrary.DAO;

public partial class Admin_ucControl_ucListMenuChild : System.Web.UI.UserControl
{
    TblMenuChildDAO menuChildDAO = new TblMenuChildDAO(Global.CONNECTION_STRING);
    TblMenuParrentDAO menuPaDAO = new TblMenuParrentDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadMenuChild();
            LoadMenuPA();
        }
    }

    private void LoadMenuPA()
    {
        drMenuPa.DataSource = menuPaDAO.GetAll();
        drMenuPa.DataValueField = "IDMenuParent";
        drMenuPa.DataTextField = "MenuParentName";
        drMenuPa.DataBind();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        bool flag = false;
        try
        {
            string id = Request.QueryString["id"];
            TblMenuChild menuChild = new TblMenuChild();
            menuChild.IDMenuParent = Global.ConvertToInt(drMenuPa.SelectedValue);
            menuChild.MenuChildName = txtMenuChildName.Text;
            if (id == "" || id == null)
            {
                menuChildDAO.InsUp(menuChild, 1);
            }
            else
            {
                menuChild.IDMenuChild = Global.ConvertToInt(id);
                menuChildDAO.InsUp(menuChild, 2);
            }
            flag = true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        if (flag)
        {
            txtMenuChildName.Text = string.Empty;
            loadMenuChild();
        }
    }

    private void loadMenuChild()
    {
        grdMenuChild.DataSource = menuChildDAO.GetAllMenuChild();
        grdMenuChild.DataBind();
    }
}